<template>
    <Particles id="tsparticles" :options="options" />
    <div class="formContainer">
        <h3>企业门户网站管理系统</h3>
        <el-form ref="loginFormRef" :model="loginForm" status-icon :rules="loginRules" label-width="80px"
            class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="loginForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="loginForm.password" type="password" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm()">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex';
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const store = useStore()

const loginFormRef = ref()
const loginForm = reactive({
    username: '',
    password: '',
})

const loginRules = reactive({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }]
})

const submitForm = () => {
    loginFormRef.value.validate((valid) => {
        if(valid){
            axios.post('/adminapi/user/login', loginForm).then(res=>{
                if(res.data.ActionType==="OK"){
                    // console.log(res.data)
                    store.commit('changeUserInfo', res.data.data)
                    store.commit('changeGetterRouter', false)
                    router.push("/index")
                }
                else{
                    ElMessage.error("账号和密码不匹配")
                }
            })
        }else{
            console.log('error submit!!');
            return false;
        }
    })
}

// 配置particles
const options = {
    background: {
        color: {
            value: '2d3a4b'
        }
    },
    fpsLimit: 120,
    interactivity: {
        events: {
            onClick: {
                enable: true,
                mode: 'push'
            },
            onHover: {
                enable: true,
                mode: 'repulse'
            },
            resize: true
        },
        modes: {
            bubble: {
                distance: 400,
                duration: 2,
                opacity: 0.8,
                size: 40
            },
            push: {
                quantity: 4
            },
            repulse: {
                distance: 200,
                duration: 0.4
            }
        }
    },
    particles: {
        color: {
            value: '#ffffff'
        },
        links: {
            color: '#ffffff',
            distance: 150,
            enable: true,
            opacity: 0.5,
            width: 1
        },
        collisions: {
            enable: true
        },
        move: {
            direction: 'none',
            enable: true,
            outMode: 'bounce',
            random: false,
            speed: 6,
            straight: false
        },
        number: {
            density: {
                enable: true,
                area: 800
            },
            value: 80
        },
        opacity: {
            value: 0.5
        },
        shape: {
            type: 'circle'
        },
        size: {
            random: true,
            value: 5
        }
    },
    detectRetina: true
}
</script>

<style lang="scss" scoped>
.formContainer {
    width: 500px;
    height: 250px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(00, 00, 00, 0.5);
    color: white;
    text-align: center;
    padding: 20px;
}
h3 {
    font-size: 30px;
}
.demo-ruleForm {
    margin-top: 30px;
}

::v-deep .el-form-item__label{
    color: white;
}
</style>